package com.kinghood.authoritycenter.controller.authority;


import com.kinghood.authoritycenter.common.model.dao.authority.AuthorityMenuDO;
import com.kinghood.authoritycenter.common.model.dto.authority.*;
import com.kinghood.authoritycenter.common.model.dto.common.IdListReqDTO;
import com.kinghood.authoritycenter.common.model.vo.authority.GetMenuVO;
import com.kinghood.authoritycenter.service.IAuthorityMenuService;
import com.kinghood.infrastructure.api.authority.SysLogModuleEnum;
import com.kinghood.infrastructure.aspect.CustomCacheEvict;
import com.kinghood.infrastructure.aspect.OperatorLog;
import com.kinghood.infrastructure.model.KingHoodResponseVO;
import com.kinghood.infrastructure.model.constant.redis.RedisConstant;
import com.kinghood.sharding.model.PageResponseVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * 组织
 *
 * @author zhangFanJun
 * @date 2023-12-06 10:42
 **/
@RequestMapping("/menu")
@Slf4j
@RequiredArgsConstructor
@RestController
public class MenuController {

    private final IAuthorityMenuService menuService;

    /**
     * 根据父类id，查找子分类，当父类id为null的时候，为根路径
     * 查找结果分页返回
     *
     * @param in 菜单列表信息
     * @return 菜单列表
     * @author zfj
     * @date 2023/12/18
     */
    @PostMapping("/queryPage")
    public KingHoodResponseVO<PageResponseVO<AuthorityMenuDO>> queryPageByParentId(@Valid @RequestBody MenuQueryPageReqDTO in) {
        return KingHoodResponseVO.success(menuService.queryPageByParentId(in));
    }

    /**
     * 菜单信息修改
     *
     * @param in 菜单信息
     * @return 菜单信息
     * @author zfj
     * @date 2023/12/18
     */
    @CustomCacheEvict(RedisConstant.AUTH)
    @OperatorLog(module = SysLogModuleEnum.Type.AUTHORITY_MENU,content = "菜单管理-保存菜单")
    @PostMapping("/addOrUpdateMenu")
    public KingHoodResponseVO<AuthorityMenuDO> addOrUpdateMenu(@RequestBody UpdateMenuReqDTO in) {
        if(Objects.isNull(in.getId())){
            AddMenuReqDTO reqDTO = new AddMenuReqDTO();
            BeanUtils.copyProperties(in,reqDTO);
            return KingHoodResponseVO.success(menuService.addMenu(reqDTO));
        }
        return KingHoodResponseVO.success(menuService.updateMenu(in));
    }

    /**
     * 新建菜单
     *
     * @param in 菜单信息
     * @return 菜单信息
     * @author zfj
     * @date 2023/12/18
     */
    @CustomCacheEvict(RedisConstant.AUTH)
    @OperatorLog(module = SysLogModuleEnum.Type.AUTHORITY_MENU,content = "菜单管理-新建菜单")
    @PostMapping("/addMenu")
    public KingHoodResponseVO<AuthorityMenuDO> addMenu(@RequestBody AddMenuReqDTO in) {
        return KingHoodResponseVO.success(menuService.addMenu(in));
    }

    /**
     * 获取菜单信息
     *
     * @param id 菜单信息
     * @return 菜单信息
     * @author zfj
     * @date 2023/12/18
     */
    @GetMapping("/getMenu/{id}")
    public KingHoodResponseVO<GetMenuVO> getMenu(@PathVariable("id") Long id) {
        return KingHoodResponseVO.success(menuService.getMenu(id));
    }


    /**
     * 菜单信息修改
     *
     * @param in 菜单信息
     * @return 菜单信息
     * @author zfj
     * @date 2023/12/18
     */
    @CustomCacheEvict(RedisConstant.AUTH)
    @OperatorLog(module = SysLogModuleEnum.Type.AUTHORITY_MENU,content = "菜单管理-菜单信息修改")
    @PostMapping("/updateMenu")
    public KingHoodResponseVO<AuthorityMenuDO> updateMenu(@RequestBody UpdateMenuReqDTO in) {
        return KingHoodResponseVO.success(menuService.updateMenu(in));
    }

    /**
     * 删除单个菜单
     *
     * @param in 菜单信息
     * @return 菜单信息
     * @author zfj
     * @date 2023/12/18
     */
    @CustomCacheEvict(RedisConstant.AUTH)
    @OperatorLog(module = SysLogModuleEnum.Type.AUTHORITY_MENU,content = "菜单管理-删除菜单")
    @PostMapping("/deleteMenu")
    public KingHoodResponseVO<Boolean> deleteMenu(@RequestBody IdListReqDTO in) {
        return KingHoodResponseVO.success(menuService.deleteMenu(in.getIdList()));
    }

    /**
     * 菜单排序
     *
     * @param in 菜单信息
     * @return 菜单信息
     * @author zfj
     * @date 2023/12/18
     */
    @OperatorLog(module = SysLogModuleEnum.Type.AUTHORITY_MENU,content = "菜单管理-菜单排序")
    @PostMapping("/changeSort")
    public KingHoodResponseVO<Boolean> changeSort(@Valid @RequestBody SystemMenuDragReqDTO in) {
        return KingHoodResponseVO.success(menuService.changeSort(in));
    }

    /**
     * 所有菜单，包括禁用的
     *
     * @return 菜单信息
     * @author zfj
     * @date 2023/12/18
     */
    @PostMapping("/queryAllMenuList")
    public KingHoodResponseVO<List<AuthorityMenuDO>> queryAllMenuList(@RequestBody QueryMenuListReqDTO in) {
        return KingHoodResponseVO.success(menuService.queryAllMenuList(in));
    }

    /**
     * 用户权限范围的菜单
     * （由于可以选择子菜单，而不选择父菜单，所以需要考虑链路返回）
     *
     * @return 菜单信息
     * @author zfj
     * @date 2023/12/18
     */
    @PostMapping("/queryMenuListByUserId")
    public KingHoodResponseVO<List<AuthorityMenuDO>> queryMenuListByUserId(@RequestBody QueryMenuListReqDTO in){
        return KingHoodResponseVO.success(menuService.queryMenuListByUserId(in));
    }

}
